package com.wzzc.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.wzzc.bean.Msg;
import com.wzzc.service.UserService;

@Controller
public class FileController {

	@Autowired
	private UserService userService;
	
	/**
	 * 文件上传
	 * @param file
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/fileUpload",method=RequestMethod.POST)
	public Msg fileUpload(@RequestParam("file")CommonsMultipartFile file,HttpServletRequest request) {
		try {
			String path=request.getServletContext().getRealPath("files/"+file.getOriginalFilename());
			File targetFile=new File(path);
			file.transferTo(targetFile);
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail(null);
		}
		return Msg.success(null);
	}
	
	/**
	 * 数据导入
	 * @param fileName
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/importStudent",method=RequestMethod.POST)
	public Msg importStudent(@RequestParam("fileName")String fileName,HttpServletRequest request) {
		String path=request.getServletContext().getRealPath("files/"+fileName);
		File file=new File(path);
		Map<String, List<Integer>> readExcelToDB = userService.readExcelToDB(file);
		if (readExcelToDB==null) {
			return Msg.fail(null);
		}
		return Msg.success(null);
	}
	
	/**
	 * 模板下载
	 * @param request
	 * @return
	 */
	@RequestMapping("/template")
	public ResponseEntity<byte[]> fileDownload(HttpServletRequest request) {
		ResponseEntity<byte[]> entity=null;
		InputStream is=null;
		try {
			String path=request.getServletContext().getRealPath("/static/template/template.xlsx");
			File file = new File(path);
			System.out.println(path);
			byte[] body = null;
			is = new FileInputStream(file);   
			body = new byte[is.available()];
			is.read(body);
			HttpHeaders headers = new HttpHeaders();
			headers.add("Content-Disposition", "attchement;filename=" + file.getName());
			HttpStatus statusCode = HttpStatus.OK;
			entity = new ResponseEntity<byte[]>(body, headers, statusCode);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if (is!=null) {
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	    return entity;
	}
}
